﻿namespace KeRui.Common
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class Discuz
    {
        public static string AuthCode(string data, string operation, string key, int expiry)
        {
            int num5;
            int num7;
            string str = DateTime.Now.Ticks.ToString();
            long num = Convert.ToInt64(str.Substring(1, 10)) - 0x7f4b0f80L;
            string str2 = "0." + str.Substring(11, 6) + "00 " + num.ToString();
            int length = 4;
            key = md5(key);
            string str3 = md5(key.Substring(0, 0x10));
            string str4 = md5(key.Substring(0x10, 0x10));
            string str5 = md5(str2);
            str5 = (length > 0) ? ((operation == "DECODE") ? data.Substring(0, length) : str5.Substring(0x1c)) : "";
            string str6 = str3 + md5(str3 + str5);
            int num3 = str6.Length;
            byte[] buffer = null;
            if (operation == "DECODE")
            {
                data = data.Substring(length);
                try
                {
                    buffer = Convert.FromBase64String(data);
                }
                catch
                {
                    try
                    {
                        buffer = Convert.FromBase64String(data + "=");
                    }
                    catch
                    {
                        try
                        {
                            buffer = Convert.FromBase64String(data + "==");
                        }
                        catch
                        {
                            return "";
                        }
                    }
                }
            }
            else
            {
                long num10 = (expiry > 0) ? (expiry + num) : 0L;
                data = num10.ToString().PadLeft(10, '0') + md5(data + str4).Substring(0, 0x10) + data;
                buffer = Encoding.UTF8.GetBytes(data);
            }
            int num4 = buffer.Length;
            string str7 = "";
            byte[] bytes = new byte[num4];
            int[] numArray = new int[0x100];
            for (num5 = 0; num5 < numArray.Length; num5++)
            {
                numArray[num5] = num5;
            }
            int[] numArray2 = new int[0x100];
            for (num5 = 0; num5 < 0x100; num5++)
            {
                numArray2[num5] = str6[num5 % num3];
            }
            num5 = 0;
            int index = 0;
            while (num5 < 0x100)
            {
                index = ((index + numArray[num5]) + numArray2[num5]) % 0x100;
                num7 = numArray[num5];
                numArray[num5] = numArray[index];
                numArray[index] = num7;
                num5++;
            }
            int num8 = 0;
            index = 0;
            for (num5 = 0; num5 < num4; num5++)
            {
                num8 = (num8 + 1) % 0x100;
                index = (index + numArray[num8]) % 0x100;
                num7 = numArray[num8];
                numArray[num8] = numArray[index];
                numArray[index] = num7;
                bytes[num5] = (byte) (buffer[num5] ^ numArray[(numArray[num8] + numArray[index]) % 0x100]);
            }
            if (operation == "DECODE")
            {
                str7 = Encoding.UTF8.GetString(bytes);
                long num9 = Convert.ToInt64(str7.Substring(0, 10));
                if (((num9 == 0L) || ((num9 - num) > 0L)) && (str7.Substring(10, 0x10) == md5(str7.Substring(0x1a) + str4).Substring(0, 0x10)))
                {
                    return str7.Substring(0x1a);
                }
                return "";
            }
            str7 = Encoding.UTF8.GetString(bytes);
            return (str5 + Convert.ToBase64String(bytes).Replace("=", ""));
        }

        public static string GetCookie()
        {
            string cookie = KeRui.Common.Utils.GetCookie(ConfigHelper.GetConfigString("CookieName"));
            if (cookie == "")
            {
                return "\t\t-1";
            }
            try
            {
                cookie = AuthCode(cookie, "DECODE", ConfigHelper.GetConfigString("Passwordkey"), 0);
            }
            catch
            {
                cookie = "";
            }
            if (cookie == "")
            {
                return "\t\t-1";
            }
            return cookie;
        }

        public static string md5(string str)
        {
            MD5 md = MD5.Create();
            string str2 = "";
            byte[] buffer = md.ComputeHash(Encoding.Default.GetBytes(str));
            for (int i = 0; i < buffer.Length; i++)
            {
                str2 = str2 + buffer[i].ToString("x2");
            }
            return str2;
        }

        public static void WriteCookie(int uid, string password, int expires)
        {
            string strValue = AuthCode(password + "\t\t" + uid.ToString(), "ENCODE", ConfigHelper.GetConfigString("Passwordkey"), 0);
            KeRui.Common.Utils.WriteCookie(ConfigHelper.GetConfigString("CookieName"), strValue, expires, ConfigHelper.GetConfigString("CookieDomain"));
        }
    }
}

